package com.itheima.controller;

import com.itheima.pojo.Score;
import com.itheima.service.ScoreService;
import com.itheima.utils.Code;
import com.itheima.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author huangbin
 * @date 2022/6/6 16:15
 */
@RestController
@RequestMapping("/score")
public class ScoreController {
    @Autowired
    private ScoreService scoreService;

    /**
     * 查询所有
     */
    @GetMapping
    public Result selectAll() {
        List<Score> result = scoreService.selectAll();

        Integer code =  result != null? Code.GET_OK : Code.GET_ERR;
        String msg = result != null?  "查询成功" : "查询失败";
        return new Result(code,msg,result);
    }

    /**
     * 查询单个
     */
    @GetMapping(value = "/{id}")
    Result selectById(@Param("id") int id) {
        Score result = scoreService.selectById(id);
        Integer code =  result != null? Code.GET_OK : Code.GET_ERR;
        String msg = result != null?  "查询成功" : "查询失败";
        return new Result(code,msg,result);
    }

    /**
     * 添加店铺
     */
    @PostMapping
    public Result add(@RequestBody Score score) {
        Integer result = scoreService.add(score);
        Integer code =  result != 0? Code.SAVE_OK : Code.SAVE_ERR;
        String msg = result != 0?  "查询成功" : "查询失败";
        return new Result(code,msg,result);
    }

    @DeleteMapping
    Result del(int id) {
        Integer result = scoreService.del(id);
        Integer code =  result != 0? Code.GET_OK : Code.GET_ERR;
        String msg = result != 0?  "删除成功" : "删除失败";
        return new Result(code,msg,result);
    }

    @PutMapping
    Result update(@RequestBody Score score) {
        Integer result = scoreService.update(score);
        Integer code =  result != 0? Code.GET_OK : Code.GET_ERR;
        String msg = result != 0?  "查询成功" : "查询失败";
        return new Result(code,msg,result);
    }
}
